package com.basketball.core.config;

import com.basketball.core.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author xzx
 */
@Configuration
public class WebAppConfigurer extends WebMvcConfigurationSupport {

    @Bean
    public TokenInterceptor tokenInterceptor(){
        return new TokenInterceptor();
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/profile/**","/static/**","/exam/login", "/exam/gettoken","/exam/district/list","/exam/model/list","/exam/banner/list","/pc/exam/**","/pc/exam/login","/pc/exam/review/**")
                .excludePathPatterns("/swagger-ui.html/**", "/v2/**", "/webjars/**", "/swagger-resources/**");
        super.addInterceptors(registry);
    }

    /**
     * 解决swagger-ui 无法访问
     * 两个方法都需要重写，只加任何一个都无法生效
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        /** 本地文件上传路径 */
        //registry.addResourceHandler("/profile/**").addResourceLocations("file:///"+ExamConfig.getUploadPath());
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
//        registry.addResourceHandler("/files/**")
//                .addResourceLocations("file:///"+staticImagePath.getUploadPath());
        registry.addResourceHandler("/static/**").addResourceLocations(
                "classpath:/static/");
    }

    /**
     * 跨域支持
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
                .maxAge(3600 * 24);
    }
}
